import java.util.Deque;
import java.util.LinkedList;

public class LeetCode150 {
    public int evalRPN(String[] tokens) {
        //创建一个栈
        LinkedList<Integer> stack = new LinkedList<>();
        //循环添加进栈
        for (String token : tokens) {
            //当遇到运算符时 ，从栈中取两个数字出来
            //注意栈的特性是 后进先出
            switch (token) {
                case "+" -> {
                    Integer b = stack.pop();
                    Integer a = stack.pop();
                    stack.push(a+b);
                }
                case "-" -> {
                    Integer b = stack.pop();
                    Integer a = stack.pop();
                    stack.push(a-b);
                }
                case "*" -> {
                    Integer b = stack.pop();
                    Integer a = stack.pop();
                    stack.push(a*b);
                }
                case "/" -> {
                    Integer b = stack.pop();
                    Integer a = stack.pop();
                    stack.push(a/b);
                }
                default -> {
                    //数字
                    stack.push(Integer.parseInt(token));
                }
            }
        }
        //弹出最后的结果
        return stack.pop();
    }
}
